package com.lee.message.core;

import com.lee.message.core.config.MqTransactionProperties;
import com.lee.message.core.aspectj.TransactionalMessageAspect;
import com.lee.message.core.transaction.CustomRocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 启动类
 */
@Configuration
@EnableConfigurationProperties(MqTransactionProperties.class)
@ConditionalOnProperty(prefix = MqTransactionProperties.PREFIX, name = "enabled", havingValue = "true")
public class MqTransactionAutoConfiguration {

    @Bean
    public TransactionalMessageAspect transactionalMessageAspect(){
        return new TransactionalMessageAspect();
    }

    /**
     * 没有监听则创建
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(RocketMQLocalTransactionListener.class)
    public RocketMQLocalTransactionListener customRocketMQLocalTransactionListener(){
        return new CustomRocketMQLocalTransactionListener();
    }


}
